package com.fjsh.utils;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.fjsh.bean.LoginUser;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.stereotype.Component;
import org.springframework.util.PatternMatchUtils;
import java.util.Collection;

@Component(value = "auth")
public class PermissionUtil {
    /**
     * 所有权限标识
     */
    private static final String ALL_PERMISSION = "*:*:*";

    /**
     * 管理员角色权限标识
     */
    private static final String SUPER_ADMIN = "admin";

    /**
     * 角色分隔符
     */
    private static final String ROLE_SEPARATOR = ",";

    /**
     * 权限分隔符
     */
    private static final String AUTHORIZE_SEPARATOR = ",";

    /**
     * 验证用户是否具备某权限
     *
     * @param anyAuthorize 权限字符串
     * @return 用户是否具备某权限
     */
    public boolean hasAuthorize(String anyAuthorize) {
        if (StrUtil.isEmpty(anyAuthorize)) {
            return false;
        }
        LoginUser loginUser = SecurityUtils.getLoginUser();
        if (ObjectUtil.isEmpty(loginUser) || CollectionUtil.isEmpty(loginUser.getAuthorities())) {
            return false;
        }
        return hasAuthorizes(loginUser.getAuthorities(), anyAuthorize);
    }

    /**
     * 验证用户是否不具备某权限，与 hasAuthorize 逻辑相反
     *
     * @param anyAuthorize 权限字符串
     * @return 用户是否不具备某权限
     */
    public boolean hasNotAuthorize(String anyAuthorize) {
        return hasAuthorize(anyAuthorize) != true;
    }

    /**
     * 验证用户是否具有以下任意一个权限
     *
     * @param anyAuthorizes 以 AUTHORIZE_NAMES_SEPARATOR 为分隔符的权限列表
     * @return 用户是否具有以下任意一个权限
     */
    public boolean hasAnyAuthorize(String anyAuthorizes) {
        if (StrUtil.isEmpty(anyAuthorizes)) {
            return false;
        }
        LoginUser loginUser = SecurityUtils.getLoginUser();
        if (ObjectUtil.isEmpty(loginUser) || CollectionUtil.isEmpty(loginUser.getAuthorities())) {
            return false;
        }
        Collection<? extends GrantedAuthority> authorities = loginUser.getAuthorities();
        for (String anyAuthorize : anyAuthorizes.split(AUTHORIZE_SEPARATOR)) {
            if (anyAuthorize != null && hasAuthorizes(authorities, anyAuthorize)) {
                return true;
            }
        }
        return false;
    }

    /**
     * 判断用户是否拥有某个角色
     *
     * @param role 角色字符串
     * @return 用户是否具备某角色
     */
    public boolean hasRole(String role) {
        if (StrUtil.isEmpty(role)) {
            return false;
        }
        LoginUser loginUser = SecurityUtils.getLoginUser();
        if (ObjectUtil.isEmpty(loginUser) || CollectionUtil.isEmpty(loginUser.getAuthorities())) {
            return false;
        }
        for (GrantedAuthority authorities : loginUser.getAuthorities()) {
            String roleKey = authorities.getAuthority();
            if (SUPER_ADMIN.contains(roleKey) || roleKey.contains(role)) {
                return true;
            }
        }
        return false;
    }

    /**
     * 验证用户是否不具备某角色，与 hasRole 逻辑相反。
     *
     * @param role 角色名称
     * @return 用户是否不具备某角色
     */
    public boolean hasNotRole(String role) {
        return hasRole(role) != true;
    }

    /**
     * 验证用户是否具有以下任意一个角色
     *
     * @param roles 以 ROLE_NAMES_SEPARATOR 为分隔符的角色列表
     * @return 用户是否具有以下任意一个角色
     */
    public boolean hasAnyRoles(String roles) {
        if (StrUtil.isEmpty(roles)) {
            return false;
        }
        LoginUser loginUser = SecurityUtils.getLoginUser();
        if (ObjectUtil.isEmpty(loginUser) || CollectionUtil.isEmpty(loginUser.getAuthorities())) {
            return false;
        }
        for (String role : roles.split(ROLE_SEPARATOR)) {
            if (hasRole(role)) {
                return true;
            }
        }
        return false;
    }

    /**
     * 判断是否包含权限
     *
     * @param grantedAuthorities 权限列表
     * @param anyAuthorize  权限字符串
     * @return 用户是否具备某权限
     */
    private boolean hasAuthorizes(Collection<? extends GrantedAuthority> grantedAuthorities, String anyAuthorize) {
        return grantedAuthorities.stream()
            .map(GrantedAuthority::getAuthority)
            .filter(StrUtil::isNotEmpty)
            .anyMatch(authority -> ALL_PERMISSION.contains(authority) || PatternMatchUtils.simpleMatch(anyAuthorize, authority));
    }
}
